﻿using shop.Bll;
using shop.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using unvell.ReoGrid;
using shop.Bll.Logi;
using CommonTools;
using CommonTools.Controls;
using shop.daifa;

namespace shop.Logi
{
    public partial class RefundDocRptForm : DockContent
    {

        shop.Bll.RefundDocBll _bll = new shop.Bll.RefundDocBll();
        int _row = -1;

        GridDataBuilder<object> _builder;


        public RefundDocRptForm()
        {
            InitializeComponent();
            this.box_begin.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
            this.box_end.Value = new DateTime(DateTime.Today.Year, DateTime.Today.Month, DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month));
        }

        private async void btn_Query_Click(object sender, EventArgs e)
        {
            var pr = new RefundRegQueryDto()
            {
                Begin = box_begin.Value.Value,
                End = box_end.Value.Value,
                FUser = UserBll.CurrentUser.Name,
                UserId = UserBll.CurrentUser.DocID.ToString(),
                platform_goods_id= box_id.Text.Trim(),
                Stock= box_stock.Text.Trim(),
                RefundNo= box_RefundNo.Text.Trim(),
            };

            var res = await WaitUI.RunAsync(async () =>
            {
                return (await _bll.QueryList(pr));
            });


            _builder = new GridDataBuilder(this.grid)
                .UseData(res)
                .AutoCreateColumns()
                .UseFilter();

            var rng = _builder.End();

            this.grid.CurrentWorksheet.HideColumns(0,1);

            if (res == null || res.Any() == false )
            {
                _row = 0;
                MessageBox.Show("没有查询到数据", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                _row = res.Count();
            }
        }



        private void btn_selectall_Click(object sender, EventArgs e)
        {
            if(_row<= 0)
            {
                MessageBox.Show("请先查询!", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);

                return;
            }


            for (int i = 1;i<= _row; i++)
            {
                grid[1, i] = true;
            }
        }


        private void grid_RangeEndEdit(object sender, unvell.ReoGrid.Events.RangeEventArgs e)
        {
            if(e.Range == null)
            {
                return;
            }

            //if(_builder == null)
            //{
            //    return;
            //}

           

        }

        private async void btn_delete_Click(object sender, EventArgs e)
        {

            var list = new List<int>();

            for (int i = grid.BindingStartRow; i < this.grid.CurrentWorksheet.RowCount; i++)
            {
                if (grid[1,i].IsNull(false) == true)
                {
                    list.Add(grid[0, i].IsNull(0));
                }
            }

            if(list.Count == 0)
            {
                MessageBox.Show("请勾选需要操作的项目", Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if(MessageBox.Show($"确认要删除{list.Count}条数据", Properties.Resources.systemInformation, MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                != DialogResult.Yes)
            {
                return;
            }

            var res = await _bll.DeleteItems(list);
            if (res.IsOk)
            {
                // 刷新
                this.btn_Query_Click(null, e);
            }
            else
            {
                MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }

        }

        private async void btnImport_Click(object sender, EventArgs e)
        {
            var dt = ExcelHelper.Import();

            if(dt == null || dt.Rows.Count ==0 )
            {
                return;
            }

            var res = await WaitUI.RunAsync(async () => await _bll.ImportData(dt));
            //var res = await _bll.ImportData(dt);

            if (res.IsOk)
            {
                MessageTip.ShowOk(this, "导入完成, 请重新查询!");
            }
            else
            {
                MessageBox.Show(res.Message, Properties.Resources.systemInformation, MessageBoxButtons.OK, MessageBoxIcon.Information);
            }



        }
    }
}
